package com.baizw.cultivation.util;

/**
 * @Description: 二进制计算工具类
 * @Author: felix
 * @Date: 2019/5/15 16:56
 * @version: V1.0
 */
public class BinaryUtil {
    
    /**
     * 获取指定位的值
     *
     * @param operate 综合码
     * @param bit     指定位
     * @return 当前位的值 1:true  0：false
     */
    public static boolean getNow(int operate, int bit) {
        int newOperate = ~(1 << bit) & operate;
        return operate != newOperate;
    }
    
    /**
     * 修改指定位的值
     *
     * @param operate 综合码
     * @param bit     指定位
     * @return
     */
    public static int modifyBit(int operate, int bit) {
        if (getNow(operate, bit)) {
            operate = ~(1 << bit) & operate;
        } else {
            operate = operate | (1 << bit);
        }
        return operate;
    }
    
    /**
     * 将对应位置为value
     *
     * @param operate 综合码
     * @param bit     指定位
     * @param value   目标值  1: true  0:false
     * @return
     */
    public static int setBit(int operate, int bit, boolean value) {
        if (value != getNow(operate, bit)) {
            operate = modifyBit(operate, bit);
        }
        return operate;
    }
    
    public static void main(String[] args) {
        System.out.println(setBit(0, 1,true));
    }
    
}
